package AdminBD;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import adminHibernate.HibernateConnection;

import entidadesHibernate.Domicilio;
import entidadesHibernate.Provincia;
import entidadesHibernate.Titular;

public class DAODomicilioMySql implements DAODomicilio {

	@SuppressWarnings("unchecked")
	public List<Provincia> obtenerProvincias(){
		
		Session session = HibernateConnection.conectar();	
			
		String hql = "from Provincia";
		
		List <Provincia> registros = new ArrayList<Provincia>();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			registros = query.list();													
			session.getTransaction().commit();		
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}
		return registros;	
		
	}

	public void guardarDomicilio(Domicilio domicilio) {
		
		Session session = HibernateConnection.conectar();
		
		try { 
			session.beginTransaction();			
			session.save(domicilio);
			session.getTransaction().commit();	
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
		}
		
	}

	public Domicilio getDomicilio(Domicilio domicilio) {
		
		String hql = "from Domicilio where id = (select max(id) from Domicilio)";		
		
		Session session = HibernateConnection.conectar();
		
		Domicilio domicilioConId = null;
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			domicilioConId = (Domicilio) query.uniqueResult();
			session.getTransaction().commit();							
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}
		
		return domicilioConId;
		
	}
	
}
